package com.credit.util;

import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.Base64;

import javax.imageio.ImageIO;

import org.springframework.web.multipart.MultipartFile;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class ImageUtil {
	
	
	public static String imgToBase64String(final RenderedImage img, final String formatName)
	{
	  final ByteArrayOutputStream os = new ByteArrayOutputStream();

	  try
	  {
	    ImageIO.write(img, formatName, os);
	    return Base64.getEncoder().encodeToString(os.toByteArray());
	  }
	  catch (final IOException ioe)
	  {
	    throw new UncheckedIOException(ioe);
	  }
	}
	
	//图片转化成base64字符串  
    public static String getImageStr(BufferedImage buffImg) throws IOException  
    {
		return null;//将图片文件转化为字节数组字符串，并对其进行Base64编码处理  
        
    /*	ByteArrayOutputStream baos = new ByteArrayOutputStream();
    	
    	ImageIO.write(buffImg, "png", baos);
    	
    	byte[] bytes = baos.toByteArray();
    	
        //对字节数组Base64编码  
       // BASE64Encoder encoder = new BASE64Encoder();  
        return encoder.encode(bytes);//返回Base64编码过的字节数组字符串  
*/   
    	
    }  
    
  //图片转化成base64字符串  
/*    public static String getImageStr(BufferedImage buffImg)  
    {//将图片文件转化为字节数组字符串，并对其进行Base64编码处理  
        InputStream in = null;  
        byte[] data = null;  
        //读取图片字节数组  
        try   
        {   File file = new File(buffImg);
        
        buffImg.get
        
            in = file.getInputStream();          
            data = new byte[in.available()];  
            in.read(data);  
            in.close();  
        }   
        catch (IOException e)   
        {  
            e.printStackTrace();  
        }  
        //对字节数组Base64编码  
        BASE64Encoder encoder = new BASE64Encoder();  
        return encoder.encode(data);//返回Base64编码过的字节数组字符串  
    } */ 

      
    //base64字符串转化成图片  
   /* public void  generateImage(String imgStr)  
    {    try {
        byte[] bytes1 = decoder.decodeBuffer(imgStr);
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
        BufferedImage bi1 = ImageIO.read(bais);
        File f1 = new File("d://out.jpg");
        ImageIO.write(bi1, "jpg", f1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    }  
*/
}
